import {
  HomeOutlined,
  TeamOutlined,
  SafetyOutlined,
  AppstoreOutlined,
  BookOutlined,
  ApartmentOutlined,
  IdcardOutlined,
  FileTextOutlined,
} from '@nbase/ui/icons';

/**
 * 路由元信息
 */
export interface RouteMetaInfo {
  /** 路由标题 */
  title: string;
  /** 路由图标 */
  icon?: React.ComponentType;
  /** 父级路由路径 */
  parent?: string;
  /** 是否隐藏面包屑 */
  hideBreadcrumb?: boolean;
}

/**
 * 路由配置映射
 * 用于生成面包屑导航和页面标题
 */
export const ROUTE_META_MAP: Record<string, RouteMetaInfo> = {
  // 首页
  '/': {
    title: '首页',
    icon: HomeOutlined,
  },

  // 系统管理
  '/system': {
    title: '系统管理',
  },
  '/system/user': {
    title: '用户管理',
    icon: TeamOutlined,
    parent: '/system',
  },
  '/system/role': {
    title: '角色管理',
    icon: SafetyOutlined,
    parent: '/system',
  },
  '/system/menu': {
    title: '菜单管理',
    icon: AppstoreOutlined,
    parent: '/system',
  },
  '/system/dict': {
    title: '字典管理',
    icon: BookOutlined,
    parent: '/system',
  },
  '/system/dept': {
    title: '部门管理',
    icon: ApartmentOutlined,
    parent: '/system',
  },
  '/system/post': {
    title: '岗位管理',
    icon: IdcardOutlined,
    parent: '/system',
  },
  '/system/audit-log': {
    title: '审计日志',
    icon: FileTextOutlined,
    parent: '/system',
  },
};

/**
 * 根据路径获取路由元信息
 */
export const getRouteMeta = (path: string): RouteMetaInfo | undefined => {
  return ROUTE_META_MAP[path];
};

/**
 * 根据路径生成面包屑数据
 */
export const generateBreadcrumbs = (path: string): Array<{ name: string; path?: string }> => {
  const breadcrumbs: Array<{ name: string; path?: string }> = [];

  // 获取当前路由的元信息
  const currentMeta = getRouteMeta(path);
  if (!currentMeta) {
    return breadcrumbs;
  }

  // 如果不是首页，先添加首页
  if (path !== '/') {
    breadcrumbs.push({
      name: '首页',
      path: '/',
    });
  }

  // 递归获取父级路由
  const buildBreadcrumbs = (currentPath: string) => {
    const meta = getRouteMeta(currentPath);
    if (!meta) return;

    // 如果有父级，先处理父级
    if (meta.parent) {
      buildBreadcrumbs(meta.parent);
    }

    // 添加当前路由（跳过首页，因为已经添加过了）
    if (currentPath !== '/') {
      breadcrumbs.push({
        name: meta.title,
        path: currentPath === path ? undefined : currentPath, // 当前页面不需要链接
      });
    }
  };

  buildBreadcrumbs(path);
  return breadcrumbs;
};

